package com.maozzi.service.acl.domain.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maozzi.service.acl.domain.PermissionDomainService;
import com.maozzi.service.acl.repository.mapper.PermissionMapper;
import com.maozzi.service.acl.repository.model.Permission;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限 - 权限/资源 domain service
 *
 * @author maozi
 */
@Service
public class PermissionDomainServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionDomainService {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Permission getPermission(Long permissionId) {
        return permissionMapper.selectById(permissionId);
    }

    @Override
    public List<Permission> getPermissions(List<Long> permissionIds) {
        return permissionMapper.selectList(Wrappers.lambdaQuery(Permission.class)
                .in(Permission::getId, permissionIds)
                .eq(Permission::getDeleted, Boolean.FALSE)
        );
    }

    @Override
    public Permission putPermission(Permission permission) {
        permission.setId(null);
        permission.setCreateDate(null);
        permission.setModifiedDate(null);
        permission.setModifiedBy(null);
        permissionMapper.insert(permission);
        return permission;
    }

    @Override
    public Boolean editPermission(Long permissionId, Permission permission) {
        return permissionMapper.update(permission,
                Wrappers.lambdaUpdate(Permission.class).eq(Permission::getId, permissionId)
        ) > 0;
    }
}
